#!/bin/bash

# script to control startup apps that reside in $XDG_CONFIG_HOME/autostart.
# 01micko 131217, ui enhancements by zigbert

# lists
LIST1=`ls $XDG_CONFIG_HOME/autostart|grep \.desktop$`
LIST2=`ls $XDG_CONFIG_HOME/autostart|grep \.bak$`
export TMP1=/tmp/${RANDOM}1
export TMP2=/tmp/${RANDOM}2
echo -n '' >$TMP1
echo -n '' >$TMP2
 
#get_enabled
for i in $LIST1
 do
 ENTRY1=`echo $i|cut -d '.' -f1`
 echo "$ENTRY1" >> $TMP1
 done

#get_disabled(){
for j in $LIST2
 do
 ENTRY2=`echo $j|cut -d '.' -f1`
 echo "$ENTRY2" >> $TMP2
 done

func_sure(){
   Xdialog --title "Startup Apps" --yesno "Are you sure you want to delete \n$1 ?" 0 0
   return $?
}
export -f func_sure

#loop used for multiple selections when holding ctrl when clicking
# move to disabled
move_right() {
   if [ "$YLIST" != "" ];then
    
     for Y in $YLIST
     do
      grep -q $Y $TMP2
      if [ $? != 0 ];then
       /usr/lib/gtkdialog/box_splash -timeout 1 -bg red -text "$(gettext 'Disabling') ${Y} $(gettext 'from startup.')"
       echo $Y >> $TMP2
       sed -i "/$Y/d" $TMP1
      fi
     done
   fi
   return
}
export -f move_right

# move to enabled
move_left() {
   if [ "$NLIST" != "" ];then
     for N in $NLIST
     do
      grep -q $N $TMP1
      if [ $? != 0 ];then
       /usr/lib/gtkdialog/box_splash -timeout 1 -bg green -text "$(gettext 'Enabling') ${N} $(gettext 'at startup.')"
       echo $N >> $TMP1
       sed -i "/$N/d" $TMP2
      fi
     done
   fi
   return
}
export -f move_left

refresh_func() {
   YLIST=`cat $TMP1`
   NLIST=`cat $TMP2`
}
export -f refresh_func

#delete
del_func(){
   if [ "$YLIST" != "" ];then
     for D in $YLIST
     do
      grep -q $D $TMP1
      if [ $? = 0 ];then
       func_sure $D
       if [ "$?" -eq 0 ];then
        /usr/lib/gtkdialog/box_splash -timeout 1 -bg red -text "$(gettext 'Deleting') ${D} $(gettext 'from startup.')"
        sed -i "/$D/d" $TMP1
        [ -f $XDG_CONFIG_HOME/autostart/${D}*.desktop ] && \
        rm -f $XDG_CONFIG_HOME/autostart/${D}*.desktop
       else
        /usr/lib/gtkdialog/box_splash -timeout 1 -text "$(gettext 'Aborting deletion of') ${D}.desktop"
        return
       fi
      else /usr/lib/gtkdialog/box_splash -timeout 1 -bg red -text "$(gettext 'Error in') ${D}.desktop"
      return 1
      fi
     done
     /usr/lib/gtkdialog/box_splash -timeout 1 -text "$(gettext 'Successfully deleted') $XDG_CONFIG_HOME/autostart/${D}.desktop"
   else /usr/lib/gtkdialog/box_splash -timeout 1 -bg red -text "$(gettext 'Error reading startup list.')"
   fi
   
   return
}
export -f del_func

#add
add_func() {
   if echo ${Entry1}|grep -q ' ';then
     /usr/lib/gtkdialog/box_splash -timeout 3 -bg red -text "${Entry1} $(gettext 'is invalid.')"
   else
     /usr/lib/gtkdialog/box_splash -timeout 2 -bg green -text "$(gettext 'adding') ${Entry1} $(gettext 'to startup')"
     [ "${Entry2}" = "Optional" ] && Entry2=""
     [ "${Entry1}" ] && mkautostart "$Entry1" "$Entry2"
     grep -q "$Entry1" $TMP1 || echo "$Entry1" >> $TMP1
   fi
}
export -f add_func
#run functions to initiate vars
#get_enabled
#get_disabled
refresh_func

# note, using unicode x2212 for minus sign in gui which is a true minus symbol, 
# not a dash/hyphen, though it looks the same in geany
export wmStartups='
<window title="'$(gettext "Startup Apps")'" icon-name="gtk-refresh">
<vbox>
  '"`/usr/lib/gtkdialog/xml_info fixed startup_app.svg 60 "$(gettext "This applications controls what apps are set to start when X windows starts on your system. These apps include the notification icons in the system tray and some other hidden services that need the graphical server to be running.")"`"'
  <notebook labels="'$(gettext 'Enable, Disable or Delete')'|'$(gettext 'Add to Startup')'" space-expand="true" space-fill="true">
    <frame '$(gettext "Current Startup Apps")'>
      <vbox border-width="10" space-expand="false" space-fill="false">
        <text wrap="false" xalign="0" use-markup="true" space-expand="false" space-fill="false"><label>"'$(gettext "<b>Remove</b> an app from startup by highlighting in the left pane and pressing <b>−</b>.")'"</label></text>
        <text wrap="false" xalign="0" use-markup="true" space-expand="false" space-fill="false"><label>"'$(gettext "<b>Add</b> an app to startup by highlighting in the right pane and pressing <b>+</b>.")'"</label></text>
        <text wrap="false" xalign="0" use-markup="true" space-expand="false" space-fill="false"><label>"'$(gettext "To <b>delete</b> an app, highlight it in the left pane and press the trash icon.")' <b>'$(gettext "Use with caution.")'</b>"</label></text>
      </vbox>
     <hbox height-request="150" space-fill="true" space-expand="true">
       <hbox space-fill="true" space-expand="true">
         <tree selection-mode="3">
           <variable>YLIST</variable>
           <label>'$(gettext "Enabled apps")'</label>
           <input>cat '"$TMP1"'</input>
         </tree>
       </hbox>
       <vbox homogeneous="true" space-fill="false" space-expand="false">
         <vbox space-fill="false" space-expand="false">
           <button border-width="3">
             '"`/usr/lib/gtkdialog/xml_button-icon remove`"'
             <action>move_right</action>
             <action>refresh_func</action>
             <action>refresh:YLIST</action>
             <action>refresh:NLIST</action>
           </button>
           <button border-width="3">
             '"`/usr/lib/gtkdialog/xml_button-icon add`"'
             <action>move_left</action>
             <action>refresh_func</action>
             <action>refresh:YLIST</action>
             <action>refresh:NLIST</action>
           </button>
           <button border-width="3">
             '"`/usr/lib/gtkdialog/xml_button-icon delete`"'
             <action>del_func</action>
             <action>refresh_func</action>
             <action>refresh:YLIST</action>
             <action>refresh:NLIST</action>
           </button>
         </vbox>
       </vbox>
     <tree selection-mode="3">
       <variable>NLIST</variable>
         <label>'$(gettext "Disabled apps")'</label>
         <input>cat '"$TMP2"'</input>
     </tree>
   </hbox>
  </frame>
 
  <frame '$(gettext "Add Startup Apps")'>
    <vbox border-width="10" space-expand="false" space-fill="false">
      <text wrap="false" xalign="0" use-markup="true" space-expand="false" space-fill="false"><label>"'$(gettext "To add an app to startup type its executable name and press ")'<b>+</b>."</label></text>
      <text wrap="false" xalign="0" space-expand="false" space-fill="false"><label>"'$(gettext "Add an optional argument in the second field.")'"</label></text>
      <text wrap="false" xalign="0" use-markup="true" space-expand="false" space-fill="false"><label>"<i>'$(gettext "Special Characters need to be escaped with a backslash.")'</i>"</label></text>
    </vbox>
    <vbox space-expand="false" space-fill="false">
    <text space-expand="false" space-fill="false"><label>""</label></text>
   <hbox space-fill="true" space-expand="true">
     <entry space-fill="true" space-expand="true">
       <variable>Entry1</variable>
     </entry>
     <entry space-fill="true" space-expand="true">
       <default>Optional</default>
       <variable>Entry2</variable>
      </entry>
      <button space-expand="false" space-fill="false">
        '"`/usr/lib/gtkdialog/xml_button-icon execute_add.svg huge`"'
       <action>add_func</action>
       <action>refresh_func</action>
       <action>refresh:YLIST</action>
       <action>refresh:NLIST</action>
      </button>
    </hbox>
    </vbox>
    <text space-expand="true" space-fill="true"><label>""</label></text>
  </frame>
  </notebook>
  <hbox space-expand="false" space-fill="false">
    <button space-expand="false" space-fill="false">
      <label>'$(gettext "Cancel")'</label>
      '"`/usr/lib/gtkdialog/xml_button-icon cancel`"'
      <action>exit:Cancel</action>
    </button>
    <button space-expand="false" space-fill="false">
      <label>'$(gettext "Ok")'</label>
      '"`/usr/lib/gtkdialog/xml_button-icon ok`"'
      <action>exit:OK</action>
    </button>
  </hbox>
 </vbox>
</window>'

. /usr/lib/gtkdialog/xml_info gtk #build bg_pixmap for gtk-theme
eval $(gtkdialog -p wmStartups)

case $EXIT in
OK)
# these could have been functions in the gtkdialog but no  point since once the app closes
# and the user uses an affirmative action [OK] then the processes only need to run once.
# the delete function is different in that it is more powerful hence the warning.

# add to enabled
while read line1; do
 [ -f $XDG_CONFIG_HOME/autostart/${line1}.desktop.bak ] && \
 mv $XDG_CONFIG_HOME/autostart/${line1}.desktop.bak $XDG_CONFIG_HOME/autostart/${line1}.desktop
 done < $TMP1
# add to disabled
while read line2; do
 [ -f $XDG_CONFIG_HOME/autostart/${line2}.desktop ] && \
 mv $XDG_CONFIG_HOME/autostart/${line2}.desktop $XDG_CONFIG_HOME/autostart/${line2}.desktop.bak
 done < $TMP2
rm $TMP1;rm $TMP2;; #cleanup
*)rm $TMP1;rm $TMP2 #cleanup
exit ;;
esac 
